import type { PayloadAction } from '@reduxjs/toolkit';
import { createSlice } from '@reduxjs/toolkit';
import type { InitialStateType } from '../../types/socketType';

const initialState: InitialStateType = {
  wsStatus: false,
  myname: null,
  id: null,
};

const wsSlice = createSlice({
  name: 'wsSlice',
  initialState,
  reducers: {
    setWsStatus: (state, action: PayloadAction<boolean>) => {
      state.wsStatus = action.payload;
    },
    setUser: (state, action: PayloadAction<null | string>) => {
      state.myname = action.payload;
    },
    setId: (state, action: PayloadAction<null | string>) => {
      if (state.id === null)
      state.id = action.payload;
    },
  },
});

export const { setWsStatus, setUser, setId } = wsSlice.actions;

export default wsSlice.reducer;
